package com.MyApp.CompanyRank;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.HashMap;

import au.com.bytecode.opencsv.*;

/**
 * import org.wikipedia.miner.model.Article; import
 * org.wikipedia.miner.model.Wikipedia; import
 * org.wikipedia.miner.util.WikipediaConfiguration;
 */
public class RankingHTML {
    public static String[] regions = { "Global", "United States", "US Liberal Arts", "United Kingdom", "Canada",
            "Australia", "GreaterChina", "China" };
    public static String[] cnRegions = { "全球", "美国", "美国文理学院", "英国", "加拿大", "澳大利亚", "中国", "中国大陆" };

    public static HashMap<String, String> regionMap = getRegionMap(regions, cnRegions);

    static HashMap<String, String> getRegionMap(String[] regions, String[] cnRegions) {
        HashMap<String, String> ret = new HashMap<String, String>();
        for (int i = 0; i < regions.length; i++) {
            ret.put(regions[i], cnRegions[i]);
        }
        return ret;
    }

//    public static String dateString = "College_2014_03";
    
    public static String dateString = "Depth4";
    
    public static String mapScriptTemplate = readEntireFile("rankingdata" + File.separator + "MapScriptTemplate.js");
    public static String RankTemplate = readEntireFile("rankingdata" + File.separator + "template"+ File.separator + "ranktemplate.html");
     public static String BingmapScriptTemplate =  readEntireFile("rankingdata"+File.separator+"MapScriptTemplate_bing.js");
    // public static String BingmapScriptTemplate = readEntireFile("rankingdata"+File.separator+"MapScriptTemplate_bing_ani.js");
//    public static String BingmapScriptTemplate = readEntireFile("rankingdata" + File.separator  + "MapScriptTemplate_bing_ani_add.js");

    static String depthString = "Depth" + UniversityData.depth;
    static String homeDir = "rankingdata" + File.separator;
    static String htmlDir = homeDir + depthString;

    // the ads files
    public static String adsDir = "rankingdata" + File.separator + "ads" + File.separator;
    public static String rightSkyscraper = readEntireFile(adsDir + "RightSkyscraper.js");
    public static String secondSkyscraper = readEntireFile(adsDir + "SecondSkyscraper.js");
    public static String rightBanner = readEntireFile(adsDir + "VerticalBanner");
    public static String bottomBanner = readEntireFile(adsDir + "BottomBanner.js");
    public static String button120 = readEntireFile(adsDir + "button120.js");
    public static String rightDisplaySky = readEntireFile(adsDir + "RightDisplaySky.js");
    public static String leftDisplaySky = readEntireFile(adsDir + "LeftDisplaySky.js");
    public static String display160 = readEntireFile(adsDir + "160DisplaySky.js");
    public static String leaderBoard = readEntireFile(adsDir + "LeaderBoard.js");
    public static String rowBanner15 = readEntireFile(adsDir + "468x15.js");
    public static String vertical240 = readEntireFile(adsDir + "Vertical240.js");
    public static String rowDisplayBanner60 = readEntireFile(adsDir + "468x60Display.js");
    public static String rowMixedBanner60 = readEntireFile(adsDir + "468x60Mixed.js");
    public static String chitika120Dynamic = readEntireFile(adsDir + "Chitika120Dynamic.js");

    public static DBpediaTable collegeTable;
    public static HashMap<String, String> nameMap;


    // public static WikiDB wikiDB = // WikiDB.getDB(WikiDB.DBType.WIKIMINERDB);
    // WikiDB.getDB(WikiDB.DBType.WIKISQLDB);;

    public static void main(String args[]) throws Exception {
        System.out.println("start Ranking HTML generation ...");
        // WikipediaConfiguration conf = new WikipediaConfiguration(new
        // File(WikiDB.wikiconfig)) ;
        // wikipedia = new Wikipedia(conf, false) ;
        // System.out.println("retrieving wiki articles ...");

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(dateFormat.format(new Date()) + " generating HTML files");

        String enfile = homeDir + File.separator + "GlobalList.txt";
        String zhfile = homeDir + File.separator + "GlobalListcn.txt";
        nameMap = UniversityData.buildEn2ZhMap(enfile, zhfile);

        String filename = homeDir + File.separator + "University.csv-old";

        // String filename =
        // homeDir+File.separator+"EducationalInstitution.csv";
        // String filename = homeDir+File.separator+"University.csv";
        collegeTable = // new DBpeadiaTable(wikiDB, new InputStreamReader(new
                       // FileInputStream(filename), "UTF-8"));
        new DBpediaTable(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
        ArrayList<UnivEntry> globalList = UniversityData.readUniversityList();
        printCollegePages(globalList);
        String[] subjects = new String[UniversityData.subjects.length + 1];
        String[] cnSubjects = new String[UniversityData.cnSubjects.length + 1];
        subjects[0] = "Overall";
        cnSubjects[0]="Overall";
        for (int i = 1; i < subjects.length; i++) {
            subjects[i] = UniversityData.subjects[i - 1];
            cnSubjects[i] = UniversityData.cnSubjects[i - 1];
        }

        for (int i = 0; i < regions.length; i++) {
            for (int j = 0; j < subjects.length; j++) {
                try {
                    printRanking(htmlDir, regions[i], subjects[j], depthString, true,cnSubjects[j]);
                    printRanking(htmlDir, regions[i], subjects[j], depthString, false);
                } catch (FileNotFoundException e) {
                    System.err.println(e);
                }
            }
        }
    }

    static void printCollegePages(ArrayList<UnivEntry> slist) throws IOException {
        String schoolPagesDir = homeDir + File.separator + "College_Pages";

        File directory = new File(schoolPagesDir);
        // check if the location exists
        if (!directory.exists()) {
            // let's try to create it
            directory.mkdir();
        }
        // print each school
        for (int i = 0; i < slist.size(); i++) {
            String se = slist.get(i).schoolName;
            String fn = RankingHTML.normalizeFileName(se);
            fn = schoolPagesDir + File.separator + fn + ".html";
            OutputStreamWriter fow = new OutputStreamWriter(new FileOutputStream(fn), "UTF-8");
            fow.append(DBpediaTable.getSchoolHTML(collegeTable, se,false));
            fow.close();

            String fn1 = RankingHTML.normalizeFileName(se);
            fn1 = schoolPagesDir + File.separator + fn1 + "_cn.html";
            OutputStreamWriter fow1 = new OutputStreamWriter(new FileOutputStream(fn1), "UTF-8");
            fow1.append(DBpediaTable.getSchoolHTML(collegeTable, se,true));
            fow1.close();
        }
    }

    public static String normalizeFileName(String filename) {
        return filename.replace('/', '_').replace('"', '_').replace(',', '_').replace('(', '_').replace(')', '_');
    }

    static UEntry[] readRanking(String fileName) throws FileNotFoundException, IOException {
        File f = new File(fileName);

        CSVReader csvReader = new CSVReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
        List<String[]> clist = csvReader.readAll();
        csvReader.close();
        if (clist == null || clist.size() == 0)
            return null;
        
        UEntry[] ulist = new UEntry[clist.size()];
        for (int i = 0; i < clist.size(); i++) {
            String[] entryString = clist.get(i);
            String entryName = entryString[1].trim();
            int score = Integer.parseInt(entryString[3]);
            UEntry entry = new UEntry(entryName, score);
            ulist[i] = entry;
        }
        return ulist;
    }
    
    static UEntry[] readPaper(String wiki1001perMajorPaper,String subject) throws FileNotFoundException, IOException {
      
        File f = new File(wiki1001perMajorPaper);
        CSVReader csvReader = new CSVReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
        List<String[]> paperList = csvReader.readAll();
        csvReader.close();
        String[] row0 = paperList.get(0);  
        UEntry[] ulist = new UEntry[paperList.size()-1];
        for (int i = 1; i < paperList.size(); i++) {
            String[] row = paperList.get(i);
            int score = -1;
            String name = row[0];
            for(int j = 0;j<row0.length;j++){
                if(row0[j].equals(subject)){  
                    score=Integer.parseInt(row[j]);
                }
            }
            if(score == -1){
                System.out.println(name);   
                System.out.println(subject);
                throw new IOException();
            }
            UEntry entry = new UEntry(name, score);
            ulist[i-1] = entry;
        }
        return ulist;
    }

    // get google map geo marker
    public static String getMapMarkers(ArrayList<UEntry> ulist, int lim,boolean isChinese) {
        String ret = "";
        for (int i = 0; i < ulist.size(); i++) {
            String geoMarker = getMapMarker(ulist.get(i),isChinese);
            if (geoMarker != null && geoMarker.length() > 0) {
                if (i == lim)
                    break;
                if (i == 0)
                    ret += geoMarker;
                else
                    ret += ",\n" + geoMarker;
            }
        }
        return ret;
    }

    // get google map HTML
    public static String getMapHTML(ArrayList<UEntry> ulist, int lim, String subject) {
        String ret = "<html><meta http-equiv=\"Content-Type\" content=\"text/html\"><meta charset=\"UTF-8\" /><head>";
        /**
         * by ider
         */
        String mapMarkers = getMapMarkers(ulist, lim,false);
        String mapScript = RankingHTML.mapScriptTemplate.replace("MARKERSXXXXX", mapMarkers);
        ret += mapScript;
        ret += "</head><body>";
        ret += "<h3><center>";
        ret += "Geographical Distribution of Top " + lim + " Universities";
        if (!subject.equals("Overall"))
            ret += " in " + subject;
        ret += "</center></h3>\n";
        ret += "<div><div id=\"map_canvas\"  class=\"mapping\" width=\"100%\" style=\"height:600px\"><p></p></div></div>\n";
        ret += "</body>"; // <center>"+RankingHTML.leaderBoard+"</center>\n";
        ret += "</html>";
        return ret;
    }

    // get bing map HTML by ider
//    public static String getBingMapHTML(ArrayList<UEntry> ulist, int lim, String subject,boolean isChinese){
//        return getBingMapHTML(ulist,lim,subject,isChinese,null);
//    }
// 
    public static String getBingMapHTML(ArrayList<UEntry> ulist, int lim, String subject,boolean isChinese,String cnSubject) {
        String ret = "<html><meta http-equiv=\"Content-Type\" content=\"text/html\"><meta charset=\"UTF-8\" /><head>";
        String mapMarkers = getMapMarkers(ulist, lim,isChinese);
        String mapScript = RankingHTML.BingmapScriptTemplate.replace("MARKERSXXXXX", mapMarkers);
        ret += mapScript;
        ret += "</head><body>";
        ret += "<h3><center>";
        if(isChinese){
            if (!subject.equals("Overall"))
                ret +=  cnSubject  ;
            ret += " 顶级 " + lim + " 所学校地理分布";
        }else{
            ret += "Geographical Distribution of Top " + lim + " Universities";
            if (!subject.equals("Overall"))
                ret += " in " + subject;
        }
        
        
        ret += "</center></h3>\n";
        ret += "<div><div id=\"myMap\"  class=\"mapping\" width=\"100%\"  style=\"position:relative;height:90%;\"><p></p></div></div>\n";
        ret += "</body>"; // <center>"+RankingHTML.leaderBoard+"</center>\n";
        ret += "</html>";
        return ret;
    }

    // get google map geo marker in the format of "[marker, geo1, geo2]"
    public static String getMapMarker(UEntry u,boolean isChinese) {
        String[] mapPoint = collegeTable.getGeoPoint(u.schoolName);
        // String date = collegeTable.getValue(u.schoolName,"foundingDate");
        String date = collegeTable.nameDate.get(u.schoolName);
        if (mapPoint == null || mapPoint.length != 2) {
            System.out.println(u.schoolName + " does not have a valid geo points");
            return "";
        }
        String un = u.schoolName.replace("'", "\\'");
        if(isChinese){
            String name_cn = collegeTable.name_cn.get(u.schoolName);
            return ("['" + un + "'," + mapPoint[0] + "," + mapPoint[1] + "," + date + ",'" + name_cn + "']");
        }else{
            return ("['" + un + "'," + mapPoint[0] + "," + mapPoint[1] + "," + date + "]");
        }
        
    }

    static ArrayList<String> getRegionList(String region) throws IOException {
        if (region.equals("US Liberal Arts") || region.equals("China") || region.equals("GreaterChina")) {
            String regionListName = homeDir + File.separator + region + "List.txt";
            ArrayList<String> selectedList = UniversityData.readRegionList(regionListName, "UTF-8");
            return selectedList;
        }
        ArrayList<String> ret = new ArrayList<String>();
        String regionListName = homeDir + File.separator + "GlobalList.txt";
        ArrayList<String> ulist = UniversityData.readRegionList(regionListName, "UTF-8");

        if (region.equals("Global")) {
            return ulist;
        }

        if (region.equals("United Kingdom")) {
            for (int i = 0; i < ulist.size(); i++) {
                String sname = ulist.get(i);
                String cname = collegeTable.getCountryName(sname);
                if (cname == null) {
                    // System.out.println(sname+" does not have a country name");
                    continue;
                }
                if (cname.equals("United Kingdom") || cname.equals("Great Britain") || cname.equals("England")
                        || cname.equals("Scotland") || cname.equals("Wales")) {
                    ret.add(sname);
                }
            }

        } else {
            for (int i = 0; i < ulist.size(); i++) {
                String sname = ulist.get(i);
                String cname = collegeTable.getCountryName(sname);
                if (cname == null) {
                    // System.out.println(sname+" does not have a country name");
                    continue;
                }
                if (cname.equals(region)) {
                    ret.add(sname);

                }
            }
        }
        return ret;
    }

    /**
     * an example would be
     * "printRanking("rankingdata" + "GreaterChina" + "Mathematics" + "Depth4");
     * 
     * @param fileDir
     * @param region
     * @param subject
     * @param depth
     */
    static void printRanking(String fileDir, String region, String subject, String depth, boolean isChinese)
            throws FileNotFoundException, IOException{
        printRanking(fileDir,region,subject,depth,isChinese,subject);
    }
    static void printRanking(String fileDir, String region, String subject, String depth, boolean isChinese,String cnSubject)
            throws FileNotFoundException, IOException {
        String parentDir = fileDir + File.separator + "..";
        String filename = parentDir + File.separator + subject + "Ranking" + depth + ".csv";
        String wiki1001perMajorPaper =parentDir + File.separator +"PaperData" +File.separator +"wiki1001perMajorPaper.csv";
        String wiki1001perMajorPaper10year =parentDir + File.separator +"PaperData" +File.separator +"wiki1001perMajorPaper10year.csv";
        
        UEntry[] ulist = readRanking(filename);
        /*添加mic论文引用数*/
        UEntry[] paperlist = null;
        UEntry[] paper10yearlist = null;
        if(!subject.equals("Overall")){
            paperlist = readPaper(wiki1001perMajorPaper,subject);
            paper10yearlist = readPaper(wiki1001perMajorPaper10year,subject);
        }
        
        ArrayList<String> selectedList = getRegionList(region);
        if (selectedList.size() == 0) {
            System.out.println("there is no school in region " + region);
        }
        /*获取论文数据*/
        ArrayList<UEntry> ret = new ArrayList<UEntry>();
        ArrayList<UEntry> ulisttemp = new ArrayList<UEntry>();
        ArrayList<UEntry> paperlisttemp = new ArrayList<UEntry>();
        ArrayList<UEntry> paper10yearlisttemp = new ArrayList<UEntry>();
        for (int i = 0; i < ulist.length; i++) {
            String n = ulist[i].schoolName;
            if (selectedList.contains(n)) {
                if(!subject.equals("Overall")){
                    for(int j=0; j<paperlist.length;j++){                
                        if(paperlist[j].schoolName.equals(n)){
                            paperlisttemp.add(paperlist[j]);
                            paper10yearlisttemp.add(paper10yearlist[j]);
                            break;
                        }
                    }
                    if(paperlisttemp.isEmpty()){
                        System.out.println("empty");
                        System.exit(0);
                    }
                }
               
                ulisttemp.add(ulist[i]);
                if(subject.equals("Overall"))
                    ret.add(ulist[i]);
            }
        }
        
        Collections.sort(paperlisttemp,new UEntryComparator());
        Collections.sort(paper10yearlisttemp,new UEntryComparator());
        Collections.sort(ulisttemp,new UEntryComparator());
        
        if(!subject.equals("Overall")){
            double paperMaxscore = paperlisttemp.get(2).score;
            double paper10yearMaxscore = paper10yearlisttemp.get(2).score;
            double uMaxscore = ulisttemp.get(2).score;
            if(paperMaxscore<=0.0) paperMaxscore = 1.0;
            if(paper10yearMaxscore<=0.0) paper10yearMaxscore = 1.0;
            if(uMaxscore<=0.0) uMaxscore=1.0;
//            System.out.println(paperMaxscore + ":" +uMaxscore + ":" + paper10yearMaxscore);
            for(int i=0;i<ulisttemp.size();i++){
                Double uscore = ulisttemp.get(i).score;
                Double paperscore = null;
                Double paper10yearscore = null;
                String name = ulisttemp.get(i).schoolName;
                for(int j=0;j<paperlisttemp.size();j++){
                    if(paperlisttemp.get(j).schoolName.equals(name)){
                        paperscore = paperlisttemp.get(j).score;
                    }
                }
                for(int j=0;j<paper10yearlisttemp.size();j++){
                    if(paper10yearlisttemp.get(j).schoolName.equals(name)){
                        paper10yearscore = paper10yearlisttemp.get(j).score;
                    }
                }
                if(paper10yearscore==null||paperscore==null){
                    System.out.print("paper is null");
                    System.exit(0);
                }
                double score = (uscore * 100000/uMaxscore) * 0.33 +(paperscore * 100000/ paperMaxscore)* 0.33 + (paper10yearscore * 100000/ paper10yearMaxscore)* 0.33;
                UEntry u = new UEntry(name,score);
                ret.add(u);
                if(region.equals("Global")&&subject.equals("Mathematics"))
                  System.out.println(name + ':' +uscore + ":" +paperscore+ ":" +paper10yearscore + ":" +score);
            }
        }


        Collections.sort(ret,new UEntryComparator());
        if (ret.size() == 0) {
            System.out.println(region + " has zero school in " + subject);
            return;
        }
        // normalize the score
        double maxScore = ret.get(0).score;
        for (int i = 0; i < ret.size(); i++) {
            int ranking = i + 1;

            double normalizedScore = 0.00;
            if (ret.get(i).score > 0)
                normalizedScore = (Math.log1p(ret.get(i).score) * 100) / Math.log1p(maxScore);

            BigDecimal bd = new BigDecimal(normalizedScore);
            bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
            normalizedScore = bd.doubleValue();

            ret.get(i).score = normalizedScore;
            if (i > 0 && (ret.get(i).score == ret.get(i - 1).score))
                ranking = ret.get(i - 1).ranking;

            ret.get(i).ranking = ranking;
        }

        String outname = fileDir + File.separator + region + subject;
        if (isChinese)
            outname += "cn";
        outname += ".html";
        OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(outname), "UTF-8");

        int lim = 500;
        String html = RankTemplate;
        String headline = "";
        if (subject.equals("Overall")) {
            if (isChinese)
                headline = regionMap.get(region) + "综合排名";
            else
                headline = region + " Overall Rankings";
        } else {
            if (isChinese){
                headline = regionMap.get(region) + " 专业排名： " + UniversityData.subjectMap.get(subject);
            }else{
                headline = region + " Subject Rankings: " + subject;
            }
        }
        html = html.replace("TITLEXXXX1", headline);
        if (region.equals("Global")) {
            if (subject.equals("Overall"))
                lim = 500;
            else
                lim = 100;
        } else {
            // if the region is not Global
            if (subject.equals("Overall")) {
                if (region.equals("United States"))
                    lim = 200;
                else
                    lim = 100;
            } else { // if in subject rankings
                if (region.equals("United States"))
                    lim = 100;
                else if (region.equals("China") || region.equals("GreaterChina"))
                    lim = 10;
                else
                    lim = 20;
            }
        }

        headline = "<h3 class=\"text-center\">" + headline;
        // print Map HTML File
        if (region.equals("Global")) {
            String mapFile = null;
            /**
             * by ider
             */
            if(isChinese){
                mapFile = fileDir + File.separator + subject + "Map_cn.html";
            }else{
                mapFile = fileDir + File.separator + subject + "Map.html";
            }
            
            OutputStreamWriter mw = new OutputStreamWriter(new FileOutputStream(mapFile), "UTF-8");
            // mw.append(getMapHTML(ret, lim, subject));
            mw.append(getBingMapHTML(ret, lim, subject,isChinese,cnSubject)); // modified by ider
            mw.close();

            /**
             * by ider
             */
            String mapurl = null;
            if(isChinese){
                mapurl = "http://knogent.com/rankingdata/" + dateString + "/" + subject + "Map_cn.html"; 
            }else{
                mapurl = "http://knogent.com/rankingdata/" + dateString + "/" + subject + "Map.html"; 
            }
            
            
            headline += "<br /><small><a href=\"" + mapurl + "\" style=\"text-decoration:none;font-size:88%\"></small>";
            if (isChinese) {
                headline += "地理分布图";
            } else
                headline += "Geographical Distribution Map";
            headline += "</a>";
        }
        headline += "</h3>";
        html = html.replace("HEADXXXX2", headline);

        printHTMLTable(ret,html, fw, lim, isChinese);
        fw.close();
    }

    static void printHTMLTable(ArrayList<UEntry> ulist,String html, Writer w, int limit, boolean isChinese) throws IOException {
        StringBuffer sb = new StringBuffer();
        sb.append("<tr>");
        sb.append("<th>");
        if (isChinese)
            sb.append("名次");
        else
            sb.append("Rank");
        sb.append("</th>");
        sb.append("<th><center>");
        if (isChinese)
            sb.append("校名");
        else
            sb.append("School Name");
        sb.append("</center></th>");
        sb.append("<th>");
        if (isChinese)
            sb.append("<br>得分");
        else
            sb.append("Score");
        sb.append("</th>");
        sb.append("</tr>\n");
        html = html.replace("THEADXXXX3", sb);
        sb = new StringBuffer();
        for (int i = 0; i < ulist.size(); i++) {
            if (i >= limit)
                break;
            UEntry ue = ulist.get(i);

            sb.append("<tr>");

            sb.append("<th>" + ue.ranking + "</th>");
            String schoolName = ue.schoolName;
            if (isChinese)
                schoolName += "<br>" + nameMap.get(schoolName);
            String slinkName = RankingHTML.normalizeFileName(ue.schoolName);
            String schoolURL = null;
            if (isChinese) {
                schoolURL = "http://knogent.com/College_Pages/" + slinkName + "_cn.html"; // by_ider
                                                                                                      
            } else {
                schoolURL = "http://knogent.com/College_Pages/" + slinkName + ".html"; // by_ider                                                                                       
            }
            sb.append("<td><a href=\"" + schoolURL + "\" style=\"text-decoration:none;\">" + schoolName + "</a></td>");

            sb.append("<td>" + ue.score + "</td>");
            sb.append("</tr>\n");

          
        }
        html = html.replace("TBODYXXXX4", sb);
        w.append(html);
    }

    public static String readEntireFile(String filename) {
        try {
            FileReader fr = new FileReader(filename);
            StringBuilder contents = new StringBuilder();
            char[] buffer = new char[4096];
            int read = 0;
            do {
                contents.append(buffer, 0, read);
                read = fr.read(buffer);
            } while (read >= 0);
            fr.close();
            return contents.toString();
        } catch (IOException ioe) {
            return "";
        }
    }
}

class UEntry implements Comparable<UEntry> {
    String schoolName = null;
    int ranking = 0;
    double score = 0;

    public UEntry(String schoolName, double score) {
        this.schoolName = schoolName;
        this.score = score;
    }

    public int compareTo(UEntry b) {
        if ((b.score - score) > 0)
            return 1;
        if ((b.score - score) == 0)
            return 0;
        return -1;
    }
}
class UEntryComparator implements Comparator<UEntry>{
    public UEntryComparator(){
    }
    public int compare(UEntry o1, UEntry o2) {
        double d1 = o1.score;
        double d2 = o2.score;

        if (d2 > d1)
            return 1;
        if (d2 < d1)
            return -1;
        return 0;
    }
}